package edu.taru.controller;

import edu.taru.pojo.Role;
import edu.taru.pojo.UserRole;
import edu.taru.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserRoleController {
    @Autowired
    RoleService userRole;
    /**
     * 插入角色
     */
    @RequestMapping(value = "/InsertRole")
    public String inser(){
        return "Role";
    }
    @RequestMapping(value = "/insert/Role")
    public String insertRole(Role role){
        userRole.addRole(role);
        return "main";
    }
    /**
     * 根据用户Id，遍历用户角色
     * @param userId
     * @param session
     * @return
     */
    @RequestMapping(value = "/user/Role")
    public String selectUserRole(String userId, HttpSession session){
        UserRole uRole=userRole.userRole(userId);
        session.setAttribute("userRole",uRole);
        return "";
    }
    /**
     * 根据角色Id，查询用户的权限
     * @param roleId
     * @param session
     * @return
     */
    @RequestMapping(value = "/role/Menu")
    public String selectUserMenu(Integer roleId,HttpSession session){
        Role uMenu=userRole.userRoleMenu(roleId);
        session.setAttribute("userMenu",uMenu);
        return "";
    }
    /**
     * 添加用户角色
     * @param role
     * @return
     */
    @RequestMapping(value = "/user/insertRole")
    public String insertRole(UserRole role){
        userRole.addUserRole(role);
        return "redirect:/select/user";
    }
    /**
     * 选择角色
     */
    @RequestMapping(value="/select/Role")
    public  String   selectRole(HttpSession session){
        List<Role> roles=userRole.selectRole();
        session.setAttribute("role",roles);
        return "UserInfo";
    }
    /**
     * 遍历角色和权限
     */
    @RequestMapping(value = "/select/Menu")
    public String selectMenu(HttpSession session){
        List<Role> roleslist=userRole.selectRoleMenu();
        session.setAttribute("listMenu",roleslist);
        return "redirect:/menu/query";
    }
}
